package net.aiform.entity.org;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.aiform.entity.IdEntity;
import net.aiform.entity.security.Sysrole;

import org.springside.modules.utils.CollectionUtils;


/**
 * Sysuser entity.
 * 
 * @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "sysuser")
public class Sysuser  extends IdEntity {

	// Fields

	private Organization organization;
	private String loginName;
	private String realName;
	private Integer isEnabled;
	private String password;
	private Integer isSuperUser;
	private Set<Sysrole> sysroles = new HashSet<Sysrole>(0);


	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ORG_ID")
	public Organization getOrganization() {
		return this.organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	@Column(name = "LOGIN_NAME", length = 60)
	public String getLoginName() {
		return this.loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	@Column(name = "REAL_NAME", length = 32)
	public String getRealName() {
		return this.realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	@Column(name = "IS_ENABLED")
	public Integer getIsEnabled() {
		return this.isEnabled;
	}

	public void setIsEnabled(Integer isEnabled) {
		this.isEnabled = isEnabled;
	}

	@Column(name = "PASSWORD", length = 128)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "IS_SUPER_USER")
	public Integer getIsSuperUser() {
		return this.isSuperUser;
	}

	public void setIsSuperUser(Integer isSuperUser) {
		this.isSuperUser = isSuperUser;
	}

	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
	@JoinTable(name = "role_user", catalog = "framework", joinColumns = { @JoinColumn(name = "ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "SYS_ID", nullable = false, updatable = false) })
	public Set<Sysrole> getSysroles() {
		return this.sysroles;
	}

	public void setSysroles(Set<Sysrole> sysroles) {
		this.sysroles = sysroles;
	}

	@Transient
	public String getRoleNames() throws Exception {
		return CollectionUtils.fetchPropertyToString(sysroles, "roleName", ", ");
	}

	@SuppressWarnings("unchecked")
	@Transient
	public List<String> getRoleIds() throws Exception {
		return CollectionUtils.fetchPropertyToList(sysroles, "id");
	}
}